/*
 * @Author: daidai
 * @Date: 2022-01-12 14:22:29
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-04-28 14:53:02
 * @FilePath: \web-pc\src\pages\big-screen\router\index.js
 */
import Vue from "vue";
import VueRouter from "vue-router";
import { isAuthenticated } from '@/utils/auth';

Vue.use(VueRouter);

const routes = [  
{
  path: '/',
  redirect: '/index',
},
{
  path: '/home',
  name: 'home',
  component: () => import(/* webpackChunkName: "LSD.bighome" */ '../views/home.vue'),
  children:[
    {
      path: '/index',
      name: 'index',
      component: () => import(/* webpackChunkName: "LSD.bighome" */ '../views/indexs/index.vue'),
    }
  ]
},
// 后台管理路由
{
  path: '/admin/login',
  name: 'AdminLogin',
  component: () => import(/* webpackChunkName: "admin" */ '../views/admin/Login.vue'),
},
{
  path: '/admin/register',
  name: 'AdminRegister',
  component: () => import(/* webpackChunkName: "admin" */ '../views/admin/Register.vue'),
},
{
  path: '/admin',
  component: () => import(/* webpackChunkName: "admin" */ '../views/admin/Layout.vue'),
  redirect: '/admin/business-stats',
  children: [
    {
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import(/* webpackChunkName: "admin" */ '../views/admin/BusinessStats.vue'),
    },
    {
      path: 'business-stats',
      name: 'BusinessStats',
      component: () => import(/* webpackChunkName: "admin" */ '../views/admin/BusinessStats.vue'),
    },
    {
      path: 'project-overview',
      name: 'ProjectOverview',
      component: () => import(/* webpackChunkName: "admin" */ '../views/admin/ProjectOverview.vue'),
    },
    {
      path: 'labeler-box-count',
      name: 'LabelerBoxCount',
      component: () => import(/* webpackChunkName: "admin" */ '../views/admin/LabelerBoxCount.vue'),
    },
    {
      path: 'qc-statistics',
      name: 'QcStatistics',
      component: () => import(/* webpackChunkName: "admin" */ '../views/admin/QcStatistics.vue'),
    },
    {
      path: 'excellent-labeler',
      name: 'ExcellentLabeler',
      component: () => import(/* webpackChunkName: "admin" */ '../views/admin/ExcellentLabeler.vue'),
    },
    {
      path: 'excellent-manager',
      name: 'ExcellentManager',
      component: () => import(/* webpackChunkName: "admin" */ '../views/admin/ExcellentManager.vue'),
    },
    {
      path: 'data-project',
      name: 'DataProject',
      component: () => import(/* webpackChunkName: "admin" */ '../views/admin/DataProject.vue'),
    },
    {
      path: 'screen-config',
      name: 'ScreenConfig',
      component: () => import(/* webpackChunkName: "admin" */ '../views/admin/ScreenConfig.vue'),
    },
    {
      path: 'labeler-management',
      name: 'LabelerManagement',
      component: () => import(/* webpackChunkName: "admin" */ '../views/admin/LabelerManagement.vue'),
    },
    {
      path: 'labeler-daily-output',
      name: 'LabelerDailyOutput',
      component: () => import(/* webpackChunkName: "admin" */ '../views/admin/LabelerDailyOutput.vue'),
    },
    
    // 供应商加盟采集报名页面
    {
      path: 'supplier-registration',
      name: 'SupplierRegistration',
      component: () => import(/* webpackChunkName: "admin" */ '../views/admin/SupplierRegistration.vue'),
    },
    
    // 供应商采集管理页面
    {
      path: 'supplier-collection',
      name: 'SupplierCollection',
      component: () => import(/* webpackChunkName: "admin" */ '../views/admin/SupplierCollection.vue'),
    },
    {
      path: 'menu-permission',
      name: 'MenuPermission',
      component: () => import(/* webpackChunkName: "admin" */ '../views/admin/MenuPermission.vue'),
    },
    {
      path: 'user-profile',
      name: 'UserProfile',
      component: () => import(/* webpackChunkName: "admin" */ '../views/admin/UserProfile.vue'),
    }
  ]
},
// 渠道页面路由 - 支持动态标题
{
  path: '/channel',
  name: 'ChannelPage',
  component: () => import(/* webpackChunkName: "admin" */ '../views/admin/SupplierRegistration.vue'),
}
];

const router = new VueRouter({
  mode: "hash",
  // base: process.env.BASE_URL,
  routes
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // 不需要认证的路由
  const publicRoutes = ['/admin/login', '/admin/register', '/', '/index', '/home', '/channel'];
  
  // 检查是否为大屏相关路由（以/index开头或为根路径）
  const isBigScreenRoute = to.path === '/' || to.path === '/index' || to.path.startsWith('/index') || to.path === '/home';
  
  if (publicRoutes.includes(to.path) || isBigScreenRoute) {
    // 公共路由直接通过
    next();
  } else {
    // 需要认证的路由
    if (isAuthenticated()) {
      // 已认证，允许访问
      next();
    } else {
      // 未认证，重定向到登录页
      next(`/admin/login?redirect=${to.path}`);
    }
  }
});

export default router;